module.exports = function ($scope, $state, $resource, $modal, toaster) {
	$scope.myKeyup = function (e) {
		//IE 编码包含在window.event.keyCode中，Firefox或Safari 包含在event.which中
		var keycode = window.event ? e.keyCode : e.which;
		if (keycode == 13) {
			$scope.loadlist();
		}
	};

	$scope.pageInfo = {};
	$scope.pageInfo.pageNo = 1;		//当前页码
	$scope.pageInfo.pageSize = 10;	//每页显示几条
	$scope.pageInfo.totalItems = 0;	//总条数
	//卡列表
	$scope.loadlist = function () {
		$resource('/api/as/mc/shopproduct/findMyProductPageList', {}, {}).save($scope.pageInfo, function (res) {
			if (res.errcode === 0) {
				$scope.beanList = res.data.results;
				$scope.pageInfo.totalItems = res.data.totalRecord;
			} else {
				toaster.error({ title: '', body: res.errmsg });
			}
		});
	}
	$scope.loadlist();

  $scope.poster = function(bean){
    var modalInstance = $modal.open({
			template: require('../views/skwxPosterList.html'),
			controller: 'skwxPosterList',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
  }

	//房间信息
	$scope.info = function (bean) {
		var modalInstance = $modal.open({
			template: require('../views/skwxMyProductInfo.html'),
			controller: 'skwxMyProductInfo',
			size: 'lg',
			resolve: {
				bean: function () {
					return bean;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function () {
			$scope.loadlist();
		});
	}

	$scope.imgclass = false;

	$scope.bigger = function () {
		$scope.imgclass = true;
	}

	$scope.smaller = function () {
		$scope.imgclass = false;
	}

	//上架
	$scope.up = function(info) {
		if(info.product_profit_set == 'product_set'){
			if (info.profit_level1 + info.profit_level2 + info.profit_level3 != 100) {
				toaster.error({title: '', body: '三级分润总和应为100%'});
				return;
			}
		} else {
			if (info.shop_profit_level1 + info.shop_profit_level2 + info.shop_profit_level3 != 100) {
				toaster.error({title: '', body: '三级分润总和应为100%'});
				return;
			}
		}
		$resource('/api/as/mc/shopproduct/updateseestateone', {}, {}).save({'id': info.id}, function(res){
				if(res.errcode === 0){
					toaster.success({title: '', body: '上架成功。'});
					$scope.loadlist();
				}else{
					toaster.error({title: '', body: '上架失败请重试。'});
				}
		});
	}

	//下架
	$scope.down = function(info) {
		var para = {
			'id': info.id
		}
		$resource('/api/as/mc/shopproduct/updateseestatezero', {}, {}).save(para, function(res){
			if(res.errcode === 0){
				toaster.success({title: '', body: '下架成功。'});
				$scope.loadlist();
			}else{
				toaster.error({title: '', body: '下架失败请重试。'});
			}
		});
  }
  $scope.setclass = function (bean) {
      var modalInstance = $modal.open({
        template: require('../views/skwxAddClass.html'),
        controller: 'skwxAddClass',
        size: 'lg',
        resolve: {
          product_code: function () {
            return bean.product_code;
          }
        }
      });
      //关闭模态框刷新页面
      modalInstance.result.then(function () {
        $scope.loadlist();
      });
	}
	//上架后的产品推送消息
  $scope.sendNotice = function(bean){
      var modalInstance = $modal.open({
        template: require('../views/sendNotice.html'),
        controller: 'sendNotice',
        size: 'lg',
        resolve: {
          bean: function () {
            return bean;
          }
        }
      });
      modalInstance.result.then(function(result) {
				
      });
  }
  $scope.addSource = function(bean){
      var modalInstance = $modal.open({
        template: require('../views/skwxAddSource.html'),
        controller: 'skwxAddSource',
        size: 'lg',
        resolve: {
          bean: function () {
            return bean;
          }
        }
      });
      //关闭模态框刷新页面
      modalInstance.result.then(function () {
        $scope.loadlist();
      });
  }
};